<template>
  <div>
    <el-table :data="competitiveTableData" stripe size="mini" style="width: 100%">
      <el-table-column prop="locationName" label="比赛场地" :width="80" align="center" />
      <el-table-column prop="phaseName" label="进程" :width="80" align="center">
        <template slot-scope="{ row }">
          <p v-if="row.phaseName === '决赛' && row.unitNum ===2">{{ '铜牌赛' }}</p>
          <p v-else>{{ row.phaseName }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="unitCode" label="场次号" :width="80" align="center" />
      <el-table-column prop="eventName" label="比赛项目" :min-width="200" />
      <el-table-column prop="statusName" label="状态" :width="110">
        <template slot-scope="{ row }">
          <p v-if="row.eventStatusCode == 'UnOfficial'" class="gl_color_success">{{ row.statusName }}</p>
          <p v-else>{{ row.statusName }}</p>
        </template>
      </el-table-column>
      <el-table-column :width="320">
        <template slot="header">
          <div class="person_wina">青方</div>
          <div class="person_vs" />
          <div class="person_winb">红方</div>
        </template>
        <template slot-scope="{ row }">
          <div class="person_wina">
            <PersonalImage v-if="row.registerTypeCodeA == 'I'" :imgurl="row.imagePathA" :imgheight="'50px'" />
            <img
              v-else
              :src="getStaticFileUrl(row.imagePathA)"
              class="person_img"
              @error="errorPhoto"
            >
            <div class="gl_mt_5">
              <div v-if="row.registerTypeCodeA == 'I'">
                <el-popover placement="top" width="300" trigger="hover" :content="row.playerNameA">
                  <div slot="reference" class="gl_ellipsis">{{ row.playerNameA ? row.playerNameA : '- -' }}</div>
                </el-popover>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationNameA">
                  <div slot="reference" class="gl_ellipsis">{{ row.delegationNameA ? row.delegationNameA : '- -' }}</div>
                </el-popover>
              </div>
              <diV v-else>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationNameA">
                  <div slot="reference" class="gl_ellipsis">{{ row.delegationNameA ? row.delegationNameA : '- -' }}</div>
                </el-popover>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.memberNamesA">
                  <div slot="reference" class="gl_ellipsis">{{ row.memberNamesA ? row.memberNamesA : '- -' }}</div>
                </el-popover>
              </diV>
              <div class="gl_color_error gl_ml_10">{{ row.combatScoreA }}</div>
            </div>
          </div>
          <div class="person_vs">VS</div>
          <div class="person_winb">
            <PersonalImage v-if="row.registerTypeCodeB == 'I'" :imgurl="row.imagePathB" :imgheight="'50px'" />
            <img
              v-else
              :src="getStaticFileUrl(row.imagePathB)"
              class="person_img"
              style="margin-left: 80px"
              @error="errorPhoto"
            >
            <div class="gl_mt_5">
              <div v-if="row.registerTypeCodeB == 'I'">
                <el-popover placement="top" width="300" trigger="hover" :content="row.playerNameB">
                  <div slot="reference" class="gl_ellipsis">{{ row.playerNameB ? row.playerNameB : '- -' }}</div>
                </el-popover>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationNameB">
                  <div slot="reference" class="gl_ellipsis">{{ row.delegationNameB ? row.delegationNameB : '- -' }}</div>
                </el-popover>
              </div>
              <div v-else>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationNameB">
                  <div slot="reference" class="gl_ellipsis">{{ row.delegationNameB ? row.delegationNameB : '- -' }}</div>
                </el-popover>
                <el-popover placement="bottom" width="300" trigger="hover" :content="row.memberNamesB">
                  <div slot="reference" class="gl_ellipsis">{{ row.memberNamesB ? row.memberNamesB : '- -' }}</div>
                </el-popover>
              </div>
              <div class="gl_color_error gl_mr_10">{{ row.combatScoreB }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="" label="比赛结果" :min-width="100">
        <template slot-scope="{ row }">
          <span v-if="row.winnerPosition == 1">青方{{ row.winTypeName }}</span>
          <span v-if="row.winnerPosition == 2">红方{{ row.winTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('table.actions')" width="330">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="openCompetitive(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="comRecord(scope.row, 'ContestResults')">生成比赛记录单</el-button>
          <el-button size="mini" type="text" @click="download(scope.row, 'RankList')">名次公告</el-button>
          <el-button size="mini" type="text" @click="download(scope.row, 'Bracket')">对阵图</el-button>
          <el-button :disabled="scope.$index === 0" type="text" size="mini" @click="clickMoveUp(scope.row, scope.$index, competitiveTableData)">上移</el-button>
          <el-button :disabled="scope.$index === competitiveTableData.length - 1" type="text" size="mini" @click="clickMoveDown(scope.row, scope.$index, competitiveTableData)">下移</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit-competitive ref="editCompetitive" @refreshPage="refreshPage()" />
  </div>
</template>
<script>
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { errorText } from '@/lib';
import { mapState, mapActions } from 'vuex';
import editCompetitive from './editCompetitive';
import PersonalImage from '@/components/PersonalImage/index';
import defaultPersonImg from '@/assets/images/face_default.png';
import {
  SiSTKWEventsManagementUpdateSessionByUnitManualInput // 更新编排手工
} from '@/lib/form/game/swaggerForm';
import {
  api_PUT_EventsManagementScheduleUnitSessionByUnitsManual
} from '@/extend/api/gameswaggerApi/app';
export default {
  name: 'Competitive',
  components: {
    editCompetitive,
    PersonalImage
  },
  props: {
    competitiveTableData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      defaultPersonImg: defaultPersonImg,
      gameId: ''
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
    this.gameId = this.$route.params.id;
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 上移
    clickMoveUp (item, index, list) {
      if (index === 0) return;
      const _currentVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _currentVO.id = item.unitId;
      _currentVO.sessionId = item.sessionId;
      _currentVO.orderInSession = list[index - 1].orderInSession;
      const _preVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _preVO.id = list[index - 1].unitId;
      _preVO.sessionId = list[index - 1].sessionId;
      _preVO.orderInSession = item.orderInSession;
      this.submitMove([_currentVO, _preVO]);
    },
    // 下移
    clickMoveDown (item, index, list) {
      const _currentVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _currentVO.id = item.unitId;
      _currentVO.sessionId = item.sessionId;
      _currentVO.orderInSession = list[index + 1].orderInSession;
      const _nextVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _nextVO.id = list[index + 1].unitId;
      _nextVO.sessionId = list[index + 1].sessionId;
      _nextVO.orderInSession = item.orderInSession;
      this.submitMove([_currentVO, _nextVO]);
    },
    // 提交位置更改
    submitMove (list) {
      api_PUT_EventsManagementScheduleUnitSessionByUnitsManual(list).then(res => {
        this.refreshPage();
      }).catch(error => {
        this.$message({
          message: `移动失败！ ${error.error.message}`,
          type: 'error',
          showClose: true
        });
      });
    },
    refreshPage() {
      this.$emit('refreshPage');
    },
    // 编辑竞技
    openCompetitive(resultInfo) {
      this.$refs.editCompetitive.open(resultInfo);
    },
    // 获取静态图片地址
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPersonImg;
    },
    /* 报表下载相关 */
    // 名次公告、对阵图
    download(row, reportName) {
      var that = this;
      if (reportName === 'RankList') {
        const tempUrl = `/api/app/events-management/report`;
        const fileName = '名次公告' + '.pdf';
        api_GET_DownLoadFileAddHeader(tempUrl, fileName, { ReportName: reportName, Format: 'PDF', CompetitionId: this.gameId, EventId: row.eventId }).then(res => {
          //
        }).catch(error => {
          errorText(error).then(function(text) {
            that.$message({ type: 'error', message: text });
          });
        });
      } else if (reportName === 'Bracket') {
        const tempUrl = `/api/app/events-management/report`;
        const fileName = '对阵图' + '.pdf';
        api_GET_DownLoadFileAddHeader(tempUrl, fileName, { ReportName: reportName, Format: 'PDF', CompetitionId: this.gameId, EventId: row.eventId, HasMatchCode: '1' }).then(res => {
          //
        }).catch(error => {
          errorText(error).then(function(text) {
            that.$message({ type: 'error', message: text });
          });
        });
      }
    },
    // 生成比赛记录单
    comRecord(row, ContestResults) {
      var that = this;
      const tempUrl = `/api/app/events-management/report`;
      const fileName = '比赛记录单' + '.pdf';
      api_GET_DownLoadFileAddHeader(tempUrl, fileName, { ReportName: ContestResults, Format: 'PDF', CompetitionId: this.gameId, UnitId: row.unitId }).then(res => {
        //
      }).catch(error => {
        errorText(error).then(function(text) {
          that.$message({ type: 'error', message: text });
        });
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.person_wina {
  display: inline-block;
  vertical-align: middle;
  width: 128px;
  text-align: left;
}
.person_vs {
  display: inline-block;
  vertical-align: middle;
  width: 39px;
  line-height: 50px;
  text-align: center;
  font-weight: bold;
}
.person_winb {
  display: inline-block;
  vertical-align: middle;
  width: 130px;
  text-align: right;
}
.person_img {
  max-width: 50px;
  max-height: 50px;
  display: block;
}
</style>
